package com.woke.addressactivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_xia;
import com.woke.diyview.XListView;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Userinfo_xiaActivity extends Activity implements XListView.IXListViewListener{

    private XListView mListView;
    private MyApp application;
    private ArrayList<Datas_xia> alllist = new ArrayList<Datas_xia>();
    private Myadapter adapter;
    private  int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo_xia);

        application = (MyApp) getApplication();
        intview();

    }

    private void intview() {
        mListView = (XListView) findViewById(R.id.userinfo_xia_listview);
        adapter = new Myadapter();
        mListView.setAdapter(adapter);
        mListView.setPullRefreshEnable(true);
        mListView.setPullLoadEnable(true);
        mListView.setXListViewListener(this);
        findViewById(R.id.userinfo_xia_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        page++;
        getdata();
    }

    private void getdata() {
        if (application.getDatas_load() == null) return;
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("g", "api");
        params.put("m", "user");
        params.put("a", "mymembers");
        params.put("userid", application.getDatas_load().getId());
        params.put("page", page);
        params.put("size", "20");
        ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e(application.getDatas_load().getId(), "" + response);
                try {
                    String flag = response.getString("flag");
                    if (flag.equals("success")) {
                        JSONObject jsonObject = response.getJSONObject("data");
                        String string = jsonObject.getString("data");
                        ArrayList<Datas_xia> datalist = (ArrayList<Datas_xia>) JSONArray.parseArray(string, Datas_xia.class);
                        alllist.addAll(datalist);
                        adapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mListView.stopRefresh();
                mListView.stopLoadMore();
            }
        });
    }

    @Override
    public void onRefresh() {
        alllist.clear();
        page=1;
        getdata();
    }

    @Override
    public void onLoadMore() {
        page++;
        getdata();
    }

    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return alllist.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        class Holder {
            TextView mText_time;
            TextView mText_lili;
            TextView mText_name;
            TextView mText_state;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                holder = new Holder();
                convertView = getLayoutInflater().inflate(R.layout.layout_include_linea1, null);
                holder.mText_time = (TextView) convertView.findViewById(R.id.include_linear_t1);
                holder.mText_lili = (TextView) convertView.findViewById(R.id.include_linear_t2);
                holder.mText_name = (TextView) convertView.findViewById(R.id.include_linear_t3);
                holder.mText_state = (TextView) convertView.findViewById(R.id.include_linear_t4);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            Datas_xia data = alllist.get(position);
            holder.mText_time.setText(data.getUser_nicename());
            holder.mText_lili.setText(data.getUser_login());
            holder.mText_name.setText(data.getVipdescribe());
            holder.mText_state.setText(data.getUserNoStateName());

            return convertView;
        }

    }

}
